package com.sgb.member.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.InputFilter
import android.text.TextUtils
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.sgb.member.R
import com.sgb.member.databinding.ActivityMemberBusinessPrice2Binding
import com.sgb.member.entity.MemberLeaseBean
import com.sgb.member.util.PointLengthFilter
import com.sgb.member.view_model.MemberBusinessPriceModel
import com.swgk.core.base.AppActivity
import com.swgk.core.dialog.DialogHelper
import com.swgk.core.util.MToast
import java.util.*
import kotlin.collections.ArrayList

/**
 * 调价页面 租赁经营
 */
class MemberBusinessPriceActivity2 : AppActivity() {
    private lateinit var viewModel: MemberBusinessPriceModel
    private lateinit var mBinding: ActivityMemberBusinessPrice2Binding
    var entity: MemberLeaseBean? = null
    var no: String? = null
    val memberlist: ArrayList<String> = ArrayList()
    companion object {
        fun jumptToAct(activity: Activity, param: Bundle) {
            val intent = Intent(activity, MemberBusinessPriceActivity2::class.java).apply {
                putExtras(param)
            }
            activity.startActivity(intent)
        }
    }

    override fun initView() {
        super.initView()
        intent.extras?.apply {
            no = getString("no")
            entity = getParcelable("bean")
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_business_price_2)
        mBinding.lifecycleOwner = this
        mBinding.bean = entity
        viewModel = ViewModelProvider(this).get(MemberBusinessPriceModel::class.java)
        mBinding.activityBar.titleView.text = "调整经营售价"
        mBinding.activityBar.ivBack.setOnClickListener { finish() }
        mBinding.tvChooseMember.setOnClickListener {
            SelectMemberPriceActivity.jumptToAct(this,memberlist)
        }
        mBinding.tvSubmit.setOnClickListener {
            val price = mBinding.etBaoJia.text.toString()
            if (TextUtils.isEmpty(price)) {
                MToast.showToast(this, "请输入报价价格")
                return@setOnClickListener
            }
            if(memberlist.size==0){
                MToast.showToast(this, "请输入会员")
                return@setOnClickListener
            }
            viewModel.QuotePriceByRent( memberlist, entity!!, price)
        }
        no?.let {
            memberlist.add(it)
            mBinding.tvChooseMember.text = "共选中${memberlist.size}个会员"
        }
        mBinding.etBaoJia.filters = arrayOf<InputFilter>(PointLengthFilter())
        registerReceiver()
    }

    private fun registerReceiver() {
        viewModel.isLoadLiveData.observe(this, { showOrHideDialog(it) })
        viewModel.msgLiveData.observe(this, {
            MToast.showLongToast(this, it)
        })
        viewModel.isSuccess.observe(this, { finish() })

    }


    fun showOrHideDialog(aBoolean: Boolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(this, "数据获取中...")
            }
        } else {
            DialogHelper.dismissProgressDialog()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 11 && resultCode == RESULT_OK) {
            data?.let {
                val list = data.extras?.getStringArrayList("list")
                list?.let {
                    mBinding.tvChooseMember.text = "共选中${it.size}个会员"
                    memberlist.clear()
                    memberlist.addAll(it)
                }
            }
        }
    }

}